package spe.ui.view;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.navigator.CommonNavigator;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

import spe.model.notation.SPEMDiagram;
import spe.model.notation.SPEMDiagramImpl;
import spe.model.spem.SPERootElement;
import spe.ui.editor.SPEMEditorInput;

public class SPENavigator extends CommonNavigator 
{
	@Override
	protected IAdaptable getInitialInput() {
		return new SPENavigatorRoot();
	}
	
	
	/*
	 * This is for testing purpose, I'm trying to 
	 * hook double-click action
	 * */
	@Override
	public void createPartControl(Composite parent)	{
		super.createPartControl(parent);
		hookDoubleClickCommand();
	}
	
	private void hookDoubleClickCommand() {
		this.getCommonViewer().addDoubleClickListener(new IDoubleClickListener() {
			
			@Override
			public void doubleClick(DoubleClickEvent event) {
				
				Object item = ((IStructuredSelection)(event.getSelection())).getFirstElement();
				System.out.println("You've just double-click on " + item.toString());
				if(item instanceof SPEMDiagramImpl)
				{
					System.out.println("[SPENavigator] Double-clicked on a diagram " + item);
					SPEMDiagram diagram = (SPEMDiagram)item;
					final IFile file = ((SPERootElement)(diagram.getParent())).getFile();
					final SPEMEditorInput input = new SPEMEditorInput(file, diagram);
					final IWorkbenchWindow ww = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
					IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
						
						@Override
						public void run(IProgressMonitor arg0) throws InvocationTargetException,
								InterruptedException {
							try{
								asynOpenEditor(input, ww);
							}catch(PartInitException pe){
								pe.printStackTrace();
							}
						}
					};
					try{
						ww.run(false, false, runnableWithProgress);
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			}
		});
	}
	
	//will be called asynchronously
	private void asynOpenEditor(SPEMEditorInput input, IWorkbenchWindow ww)throws PartInitException, InterruptedException, InvocationTargetException{
		IWorkbenchPage activePage = ww.getActivePage();
		//activePage.openEditor(input, SPEMGraphEditor.ID, false);
		activePage.openEditor(input, "spe.ui.editor.SPEMGraphEditor", false);
	}
}
